#!/bash/sh

# 变量
DEPLOY_DIR=/xdfapp/study-java-jvm
JAR_FILE=$DEPLOY_DIR/study-java-jvm-1.0.jar

# 服务运行时，停止
pid=$(ps -ef | grep -v grep | grep $JAR_FILE | awk '{print $2}')
if [ ! -z "$pid" ]; then
  kill -9 $pid >/dev/null
  sleep 2
  echo '[INFO] Service Stopped'
fi

#（★）参数
# ****** 堆 ******
#JAVA_MEM_HEAP_OPTS=" -server -Xms100M -Xmx100M"
#JAVA_MEM_HEAP_OPTS=" -server -Xms100M -Xmx100M -Xmn50M"
#JAVA_MEM_HEAP_OPTS=" -server -Xms100M -Xmx100M -XX:NewRatio=2 -XX:SurvivorRatio=8"
JAVA_MEM_HEAP_OPTS=" -Xms1024M -Xmx1024M"

# ****** 元空间 ******
JAVA_MEM_META_OPTS=" -XX:MetaspaceSize=512M -XX:MaxMetaspaceSize=512M"

# ****** 虚拟机栈 ******
JAVA_MEM_STACK_OPTS=" -Xss128K"

# GC
JAVA_GC_OPTS=" -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintHeapAtGC -Xloggc:/xdfapp/logs/study-java-jvm/gc.log"
#
JAVA_OPTS=" -server"$JAVA_MEM_HEAP_OPTS$JAVA_GC_OPTS

# 启动服务
source /etc/profile
if [ $1 -eq 0 ];then
  java $JAVA_OPTS -jar $JAR_FILE
else
  nohup java $JAVA_OPTS -jar $JAR_FILE >/dev/null 2>&1 &
fi
#
if [ $? -eq 0 ];then
  echo '[INFO] Service Started'
else
  echo '[ERROR] Service Startup Error...'
fi